package com.zty.util;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

/**
 * @author tyzhang19
 * @Description
 * @date 2022-11-08 18:12
 */
public class HttpUtils {

    String url = "https://api.inmobi.com/v1.0/generatesession/generate";
    String offerUrl = "https://api.inmobi.com/iap/v0/json?api_key=e73b2ba3-ac43-4c13-afa2-6e830125c4e7&target=affiliate_offer&method=findMyApprovedOffers";

    String username = "derek@way.io";

    String secretkey = "f438613a66df44fb86e11af364116e52";

    String accountid = "46e86b6b136943f48671cd2794dd2497";

    String sessionid = "f5da945c6b9c4a91b83c8917d3117006";


    public void getStr() throws IOException {
        HttpClient client = HttpClientBuilder.create().build();

        HttpGet request = new HttpGet(offerUrl);

//        request.addHeader("username", username);
//        request.addHeader("secretkey", secretkey);
        request.addHeader("accountid", accountid);
        request.addHeader("sessionid", sessionid);
        request.addHeader("secretkey", secretkey);

        HttpResponse response = client.execute(request);

        System.out.println("response: "+ response.getStatusLine().getStatusCode());

        if(response.getEntity().getContentLength() > 0 && response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {

            HttpEntity entity = response.getEntity();
            String s = EntityUtils.toString(entity);

            System.out.println("Response  : " + s);

//            JSONObject jsonObject = JSONObject.parseObject(s);
//            JSONArray respList = jsonObject.getJSONArray("respList");
//            JSONObject parseObject = JSONObject.parseObject(respList.get(0).toString());
//            String sessionId = parseObject.get("sessionId").toString();
//            String accountId = parseObject.get("accountId").toString();
//            System.out.println(sessionId +  "--"  + accountId);
        }

    }
}

